<template>
  <div>
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="120px">
        <el-divider content-position="left">基础设置</el-divider>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="请选择类型" prop="serviceType">
              <el-select v-model="form.serviceType" placeholder="请选择类型">
                <el-option
                  v-for="item in this.dict.application_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示图标" prop="isShowImage">
              <el-radio-group v-model="form.isShowImage">
                <el-radio
                  v-for="item in this.dict.whether_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="显示条数" prop="xsts">
              <el-input-number v-model="form.xsts" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="列宽" prop="lk">
              <el-input v-model="form.lk" placeholder="列宽" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否显示更多" prop="isMore">
              <el-radio-group v-model="form.isMore">
                <el-radio
                  v-for="item in this.dict.state_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否可配置" prop="isSet">
              <el-radio-group v-model="form.isSet">
                <el-radio
                  v-for="item in this.dict.state_type"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="查询条件" prop="queryParameter">
              <el-input v-model="form.queryParameter" placeholder="填写查询条件，如:sftj=1" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序方式" prop="orderParameter">
              <el-input v-model="form.orderParameter" placeholder="填写排序方式，如：order by fbsj desc" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="custom-button">
          <el-button type="primary" size="small" :loading="loading" icon="el-icon-circle-check" @click="saveEntity">保存
          </el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { adminQueryTemplate, adminSaveOrUpdate } from '@/api/template/serviceList'

export default {
  name: 'ServiceList',
  dicts: ['application_type', 'whether_type', 'state_type'],
  props: {
    template: {
      type: Number,
      require: true
    }
  },
  data() {
    return {
      loading: false,
      form: {
        templateId: this.template,
        serviceType: '', // 服务类型
        isShowImage: 'true', // 是否显示图标
        isMore: 'true', // 是否显示更多
        isSet: 'true', // 是否可设置
        orderParameter: '', // 排序方式
        queryParameter: '', // 查询条件
        xsts: 1, // 显示条数
        lk: ''// 列宽
      },
      defaultForm: {
        templateId: this.template,
        serviceType: '', // 服务类型
        isShowImage: 'true', // 是否显示图标
        isMore: 'true', // 是否显示更多
        isSet: 'true', // 是否可设置
        orderParameter: '', // 排序方式
        queryParameter: '', // 查询条件
        xsts: 1, // 显示条数
        lk: ''// 列宽
      }
    }
  },
  watch: {
    template: {
      handler(newName, oldName) {
        if (newName != null && undefined !== newName) {
          this.initEntity()
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initEntity() {
      adminQueryTemplate({ 'tid': this.template }).then(res => {
        if (res != null && res !== '') {
          this.form = res
          this.form.isShowImage = this.form.isShowImage.toString()
          this.form.isMore = this.form.isMore.toString()
          this.form.isSet = this.form.isSet.toString()
        } else {
          this.form = this.defaultForm
        }
      }).catch(error => {
        console.log(error)
      })
    },
    saveEntity() {
      this.loading = true
      adminSaveOrUpdate(this.form).then(res => {
        this.$message({
          message: '配置项保存成功！',
          type: 'success'
        })
        this.loading = false
      }).catch(() => {
        this.$message.error('保存失败！请联系系统管理员！')
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
.custom-button {
  width: 100%;
  height: 40px;
  text-align: center;
}
</style>
